package com.example.demo.domain.interceptor.configuration;

import com.example.demo.domain.interceptor.MyInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;
@Configuration
public class MyInterceptorConfig implements WebMvcConfigurer {
    @Autowired
    MyInterceptor myInterceptor;
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //创建不需要拦截的地址的集合
        List<String> patterns = new ArrayList<>();
        //添加不需要拦截的地址
        patterns.add("/user/login");
        patterns.add("/user/register");
        patterns.add("/user/update");
        patterns.add("/borrow/**");
        //注册我们自定义的拦截器
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/**") //所有的请求都要拦截。
                .excludePathPatterns(patterns); //将不需要拦截的接口请求排除在外
    }
}
